<?php
declare (strict_types = 1);

namespace app\model;


use think\facade\Db;
use think\Model;
use think\model\concern\SoftDelete;

/**
 * @mixin \think\Model
 */
class User extends Model
{
    use SoftDelete;
    /**
     * 获取表字段
     * @param bool $isPk 是否获取主键
     *
     * @return array
     */
    public function getFields(bool $isPk=false):array{
        $fields = array_keys(Db::table($this->getTable())->getFields());
        if(!$isPk){
            $pk = $this->getPk();
            foreach ($fields as $k=>$f){
                if($f === $pk){
                    unset($fields[$k]);
                }
            }
        }

        return $fields;
    }

    public function getHeadimgAttr(string $name):string
    {
        return addDomain($name);
    }

    public function getLastLoginTimeAttr(int $name):string {
        return $name ? date('Y-m-d H:i:s',$name) : '';
    }

    public function setRoleIdAttr($value):string {
        return is_array($value) && count($value) ? implode(',',$value) : $value;
    }

    public function getRoleIdAttr($value):array {
        return $value ? explode(',',$value) : [];
    }


}
